/*
 * @lc app=leetcode.cn id=135 lang=typescript
 *
 * [135] 分发糖果
 */

// @lc code=start
function candy(ratings: number[]): number {
    const candies = Array(ratings.length).fill(1)

    for(let i = 1; i < ratings.length; i++) {
        if (ratings[i] > ratings[i-1]) {
            candies[i] = candies[i-1] + 1
        }
    }
    for(let i = ratings.length - 2; i >= 0; i--) {
        if (ratings[i] > ratings[i+1]) {
            candies[i] = Math.max(candies[i], candies[i+1] + 1)
        }
    }

    return candies.reduce((i, sum) => sum + i, 0)
};
// @lc code=end

console.log(
    candy([1,0,2]) // 5
    // candy([1,2,2]) // 4
)